package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.app.enterprise.models.ImportExportInformation;
import com.budwk.app.enterprise.services.ImportExportInformationService;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/import/export/information")
@ApiDefinition(tag = "进出口相关信息")
@Slf4j
public class ImportExportInformationController {
    @Inject
    private ImportExportInformationService importExportInformationService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询进出口相关信息")
    @ApiFormParams(implementation = ImportExportInformation.class)
    @ApiResponses(
    implementation = ImportExportInformation.class
    )
    @SaCheckPermission("enterprise.import.export.information.listByPage")
    public Result<?> list(@Param("..")ImportExportInformation bean) {
        Pagination pagination = importExportInformationService.listByPage(bean);
        return Result.data(pagination);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增进出口相关信息")
    @ApiFormParams(
        implementation = ImportExportInformation.class
    )
    @ApiResponses
    @SLog(tag = "新增进出口相关信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.import.export.information.create")
    public Result<?> create(@Param("..")ImportExportInformation dfxyEnterprise) {
        dfxyEnterprise.setCreatedBy(SecurityUtil.getUserId());
        importExportInformationService.insert(dfxyEnterprise);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改进出口相关信息")
    @ApiFormParams(
        implementation = ImportExportInformation.class
    )
    @ApiResponses
    @SLog(tag = "修改进出口相关信息", value = "${args[0].id}")
    @SaCheckPermission("enterprise.import.export.information.update")
    public Result<?> update(@Param("..")ImportExportInformation dfxyEnterprise, HttpServletRequest req) {
        dfxyEnterprise.setUpdatedBy(SecurityUtil.getUserId());
        importExportInformationService.updateIgnoreNull(dfxyEnterprise);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取进出口相关信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.import.export.information.getData")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        ImportExportInformation dfxyEnterprise = importExportInformationService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(dfxyEnterprise);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除进出口相关信息")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除进出口相关信息", value = "${args[0]}")
    @SaCheckPermission("enterprise.import.export.information.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        ImportExportInformation dfxyEnterprise = importExportInformationService.fetch(id);
        if (dfxyEnterprise == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        importExportInformationService.delete(id);
        return Result.success();
    }
}
